﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace PracaInz_v0
{
	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : Application
	{
        public static String[] ARGS;
        TreeD TreeCMD;

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            if (e.Args.Length > 0)
            {
                ARGS = e.Args;
                CultureInfo ci = new System.Globalization.CultureInfo("en-us");
                Thread.CurrentThread.CurrentCulture = ci;

                try
                {
                    if (ARGS[0] == "1")
                    {
                        CreateTreeCMD();
                    }
                    else if (ARGS[0] == "2")
                    {
                        TestTreeCMD();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());                 
                }               
            }
        }

        void CreateTreeCMD()
        {
            TreeCMD = new TreeD();
            TreeCMD.mNV = int.Parse(ARGS[1]);       //minimalna ilosc wektorow potomka
            TreeCMD.mNVP = int.Parse(ARGS[2]);      //minimalna ilosc potonkow dla rodzica
            TreeCMD.mL = int.Parse(ARGS[3]);        //max poziom 
            TreeCMD.mVN = double.Parse(ARGS[4]);    //minimalna wariancja wezla  

            string filePath = ARGS[5];              //sciezka pliku z wektorami
            string treepath = ARGS[6];              //nazwa/sciezka do pliku wyjsciowego z drzewem
            string separator = ARGS[7];             //separator do pliku wysciowego z drzewem
            string method;

            if (ARGS.Length == 9)
                method = ARGS[8];                   //metoda: 1 = TreeMinVar, 2 = TreeMLP, 3 = TreeMinVar2A ; domyslnie 1
            else
                method = "1";

            TreeCMD.loadFileFromPath(filePath);
            TreeCMD.saveCriteria(TreeCMD.mNV.ToString(), TreeCMD.mNVP.ToString(), TreeCMD.mL.ToString(), TreeCMD.mVN.ToString());

            Task.Run(() =>
                {
                    switch (method)
                    {
                        case "1":
                            TreeCMD.Tree(new TreeViewSettings(), "TreeMinV", TreeCMD.minVar, TreeCMD.verificationNode);
                            break;
                        case "2":
                            TreeCMD.Tree(new TreeViewSettings(), "TreeMLP", TreeCMD.neuronMLP, TreeCMD.verificationNode);
                            break;
                        case "3":
                            TreeCMD.Tree(new TreeViewSettings(), "MinV2A", TreeCMD.minVar2A, TreeCMD.verificationNode);
                            break;
                        default:
                            Application.Current.Shutdown();
                            break;
                    }

                    //load_tree --> load_set --> simulation
                    TreeCMD.loadFileTreePath(TreeCMD.fileNameT);                //loadtree
                    TreeCMD.loadFileSPath(filePath);                            //loadset

                    TreeCMD.AJT = TreeCMD.symulation();
                    TreeCMD.AssignSymulationDT();
                    TreeCMD.saveTreeAllTarget(TreeCMD.AJTree, TreeCMD.AJT, treepath, separator, RegresionMean(filePath));

                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        Application.Current.Shutdown();
                    }));
                });
        }

        double[] RegresionMean(string path)
        {
            DataTable Tab = LoadData.loadFile(TreeCMD.fileNameT);
            DataTable Vect = LoadData.loadFile(path);

            double[] tab = new double[Tab.Rows.Count];

            List<Task> LT = new List<Task>();

            int i = 0;
            try
            {
                foreach (DataRow item in Tab.Rows)
                {
                    double RMean = 0;
                    if (Convert.ToDouble(item[4]) == 0 && Convert.ToDouble(item[5]) == 0)
                    {
                        string counter = item[0].ToString();                               
                        Cluster c = new Cluster(Vect, null, TreeCMD.rootDirNameC + "\\Set00" + counter + ".txt");
                        RMean = c.NewMean;                              
                    }
                    tab[i] = RMean;
                    i++;
                }                      
            }
            catch (Exception e)
            {
                MessageBox.Show("blad" + e.ToString());
            }
            Task.WaitAll(LT.ToArray());
            return tab;
        }    

        void TestTreeCMD()
        {
            Task.Run(() =>
                {
                    try
                    {
                        DataTable Tree = LoadData.loadFile(ARGS[1]);              // plik z drzewem
                        DataTable Vect = LoadData.loadFileVect2(ARGS[2]);          // plik z wektorami
                        string file = ARGS[4];                                    // nazwa / sciezka pliku utworzonego
                        string mode = ARGS[5];
                        new ExternalVectors().ClassifyNewVect_SingleTreeCMD(Tree, Vect, mode, file);   // uruchomienie funkcji szukajacej sredniego y dla tego wektora

                        this.Dispatcher.Invoke((Action)(() =>
                        {
                            Application.Current.Shutdown();
                        }));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error in execution command" + ex.ToString());
                    }
                });
        }
	}
}